package com.qiantu.phone.ui.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.ElecBayBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class EnergyStatisticsDetailActivity extends AppActivity {
    private android.widget.TextView area;
    private android.widget.TextView time;
    private androidx.recyclerview.widget.RecyclerView list;
    private String startDate;
    private String endDate;
    private boolean isAllHouse;
    private ArrayList<String> mRoomSerialNos;
    private String titleStr;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_energy_statistics_detail;
    }

    public static void start(Context context, boolean isAllHouse, ArrayList<String> mRoomSerialNos, String titleStr, String startDate, String endDate){
        Intent intent = new Intent(context, EnergyStatisticsDetailActivity.class);
        intent.putExtra("startDate", startDate);
        intent.putExtra("endDate", endDate);
        intent.putStringArrayListExtra("mRoomSerialNos", mRoomSerialNos);
        intent.putExtra("isAllHouse", isAllHouse);
        intent.putExtra("titleStr", titleStr);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        startDate = getIntent().getStringExtra("startDate");
        endDate = getIntent().getStringExtra("endDate");
        isAllHouse = getIntent().getBooleanExtra("isAllHouse", true);
        mRoomSerialNos = getIntent().getStringArrayListExtra("mRoomSerialNos");
        titleStr = getIntent().getStringExtra("titleStr");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        area = (TextView) findViewById(R.id.area);
        time = (TextView) findViewById(R.id.time);
        list = (RecyclerView) findViewById(R.id.list);
        list.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ElectDetailAdapter();
        list.setAdapter(adapter);

        area.setText(titleStr);
        time.setText(getTimeText());
    }

    private String getTimeText(){
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM/dd");
        try {
            String str1 = dateFormat2.format(Objects.requireNonNull(dateFormat1.parse(startDate)));
            String str2 = dateFormat2.format(Objects.requireNonNull(dateFormat1.parse(endDate)));
            return str1 + "~" + str2;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    protected void initData() {
        LLHttpManager.getElecStatisticsDetail(getAppActivity(), isAllHouse, AppApplication.getInstance().getCurrentHouseSerialNo(), mRoomSerialNos,
                startDate, endDate, new HttpCallback<HttpData<List<ElecBayBean>>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<List<ElecBayBean>> result) {
                super.onSucceed(result);
                adapter.setNewInstance(result.getData());
            }
        });
    }

    ElectDetailAdapter adapter;
    private class ElectDetailAdapter extends BaseQuickAdapter<ElecBayBean, BaseViewHolder>{

        public ElectDetailAdapter() {
            super(R.layout.elec_day_item_ly);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, ElecBayBean item) {
            holder.setVisible(R.id.bg, holder.getAbsoluteAdapterPosition()%2==0);
            holder.setText(R.id.date, item.date);
            holder.setText(R.id.elec, String.format("%.1f", item.elec));
            holder.setText(R.id.bill, String.format("%.2f", item.bill));
        }
    }
}